<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%--<chiptrack:checkLogon sourcePage="/sortDataEntry.do"/>--%>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>


<html>

<HEAD>
	<TITLE>ScalarSoft - ChipTrac - Sort Bin Data Entry </TITLE>
    <META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
    <LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
	<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <script language='javascript' >

	function resetValues()
    {
        document.binDataEntryform.reset();
        fetchBinData();
    }

    function save()
	{
        errors  = "";
        var pa  = document.getElementById("message");
        success = false;

        form		= document.binDataEntryform;
		openerform	= window.opener.document.sortDataEntry;
        noofwafers	= openerform.waferQuantity.value;
	    goodBinsCount = openerform.goodBinsCount.value;

        //Bhaskar added this code snippet on 5 Feb 2004 check or blank value entry of the wafer Id
		if( noofwafers > 1 )
		{
            for ( i = 0; i < noofwafers; i++ )
			{
				if ( form.waferId[i].value.trim() == "" )
				{
					errors = "Wafer Id cannot be blank for Sl No."+ ( i + 1 );
					pa.innerHTML = errors;
					success = false;
					break;
                }
            }
            if( errors == "" )
			{
                success = true;
            }
        }

		if( noofwafers > 1 )
		{
            for ( i = 0; i < noofwafers - 1; i++ )
			{
                for ( j = i + 1; j < noofwafers; j++ )
				{
                    if ( form.waferId[i].value != "" && form.waferId[j].value != "" && form.waferId[i].value == form.waferId[j].value )
					{
                        errors = "Wafer Id's of two Wafer's should not be same.";
                        pa.innerHTML = errors;
                        success = false;
                    }
                }
            }
            if( errors == "" )
			{
                success = true;
            }
        }
		else if ( noofwafers = 1 )
		{
			if ( form.waferId.value.trim() == "" )
			{
				errors = "Wafer Id cannot be blank";
				pa.innerHTML = errors;
				success = false;
            }
			if ( errors == "" )
			{
				success = true;
			}
        }
        if( success == true )
		{
			if ( !validateBinQuantities())
			{
				return;
			}
            if( noofwafers > 0 )
			{
                if( noofwafers > 1 )
				{
                    for( count = 0; count < noofwafers ; count++ )
					{
						for( binCount = 0; binCount < goodBinsCount; binCount++ )
						{
                            binValue = eval('form.bin_'+binCount)[count].value.trim();
                            if( binValue != "" )
                            {
                                if ( checkInteger( binValue , ' Bin '+(binCount+1) , 'Y'  ) == false)
                                {
                                    pa.innerHTML  = errMsg+" for SlNo"+(count+1);
                                    success = false;
                                    eval('form.bin_'+binCount)[count].focus();
                                    break;
                                }
                            }

						}
                    }
                }
				else
				{
                    for( binCount = 0; binCount < goodBinsCount; binCount++ )
					{
                        binValue = eval('form.bin_'+binCount).value.trim();
                        if( binValue != "" )
                        {
                            if ( checkInteger( binValue , ' Bin '+(binCount+1) , 'Y'  ) == false)
                            {
                                pa.innerHTML  = errMsg+" for SlNo"+1;
                                success = false;
                                eval('form.bin_'+binCount).focus();
                                break;
                            }
                        }

                    }
                }
            }

         //TO CHECK BIN TOTALS
        grossDice		= openerform.fabGrossDie.value;
        goodDieCount	= 0;
		if ( noOfWafers > 1 )
		{
            for( count = 0; count < noofwafers ; count++ )
            {
                goodDieCount	= 0;
                for( var binCount = 0; binCount < goodBinsCount; binCount++ )
                {
                    tempBinValue = eval('form.bin_'+binCount)[count];
                    if ( tempBinValue != null && tempBinValue.value != "" &&  !isNaN( tempBinValue.value ) )
                    {
                        goodDieCount =  parseInt( goodDieCount ) + parseInt( tempBinValue.value );
                    }
                }
                if ( parseInt(goodDieCount,10) > parseFloat(grossDice) )
                {
                    pa.innerHTML = "Bin total value can not be greater than Gross Die/Wafer(" + grossDice  + ") for Sl.No " + ( count + 1 );
                    success = false;
                    break;
                }
            }
        }
		else
		{
            for( var binCount1 = 0; binCount1 < goodBinsCount; binCount1++ )
			{
                tempBinValue = eval('form.bin_'+binCount1);
                if ( tempBinValue != null && tempBinValue.value.trim() != "" &&  !isNaN( tempBinValue.value ) )
				{
                    goodDieCount =  parseInt( goodDieCount ) + parseInt( tempBinValue.value );
                }
            }
            if ( parseInt(goodDieCount,10) > parseFloat(grossDice) )
            {
                pa.innerHTML = "Bin total value can not be greater than Gross Die/Wafer(" + grossDice  + ")";
                success = false;
            }
        }

            if(success)
            {
                //TO SAVE VALUES IN PARENT WINDOW
               CaculateDieOutAndShipYield();
                if( noofwafers > 0 )
                {
                    if( noofwafers > 1 )
                    {
                        for( count = 0; count < noofwafers ; count++ )
                        {
                            window.opener.updateBinId( count, form.waferId[count].value, form.binRejected[count].value, form.binYield[count].value );

                            for( binCount = 0; binCount < goodBinsCount; binCount++ )
                            {
                                //SAVE BIN VALUES
                                window.opener.updateBinValues( count, binCount, eval('form.bin_'+binCount)[count].value );
                            }
                        }
                    }
                    else
                    {
                        //SAVE VALUES
                        window.opener.updateBinId( 0 , form.waferId.value, form.binRejected.value, form.binYield.value);
                        for( binCount = 0; binCount < goodBinsCount; binCount++ )
                        {
                            window.opener.updateBinValues( 0, binCount , eval('form.bin_'+binCount).value);
                        }
                    }
                }
                window.close();
            }
		}
    }

	function fetchBinData()
	{
		form		= document.binDataEntryform;
        openerform	= window.opener.document.sortDataEntry;
        noofwafers	= openerform.waferQuantity.value;
        goodBinsCount = openerform.goodBinsCount.value;

        if( noofwafers > 0 )
		{
            if( noofwafers > 1 )
			{
                for( count = 0; count < noofwafers ; count++)
				{
                    if( openerform.waferId != null && openerform.waferId[count] != null && form.waferId[count] != null )
					{
						form.waferId[count].value = openerform.waferId[count].value;
                    }
                    if( openerform.binRejected != null && openerform.binRejected[count] != null && form.binRejected[count] != null )
					{
						form.binRejected[count].value = openerform.binRejected[count].value;
                    }

					if( openerform.binYield != null && openerform.binYield[count] != null && form.binYield[count] != null )
					{
						form.binYield[count].value = formatYieldIntoDoubleDecimalMode ( openerform.binYield[count].value );
                        //form.binYield[count].value = openerform.binYield[count].value;
                    }
					for( binCount = 0; binCount < goodBinsCount; binCount++ )
					{
                        if( eval('openerform.bin_'+binCount) != null &&  eval('openerform.bin_'+binCount)[count] != null && eval('form.bin_'+binCount)[count] != null)
						{
                              eval('form.bin_'+binCount)[count].value = eval('openerform.bin_'+binCount)[count].value;
                        }
                    }
				}
            }
			else
			{
				if( openerform.waferId != null && form.waferId != null )
				{
					if( openerform.waferId.length == null )
					{
						form.waferId.value = openerform.waferId.value;
					}
					else
					{
						form.waferId.value = openerform.waferId[0].value;
					}
				}
                if( openerform.binRejected != null && form.binRejected != null )
				{
					if( openerform.binRejected.length == null )
					{
						form.binRejected.value = openerform.binRejected.value;
					}
					else
					{
						form.binRejected.value = openerform.binRejected[0].value;
					}
				}

				if( openerform.binYield != null && form.binYield != null )
				{
					if( openerform.binYield.length == null )
					{
						form.binYield.value = formatYieldIntoDoubleDecimalMode ( openerform.binYield.value );
                        //form.binYield.value = openerform.binYield.value;
                    }
					else
					{
						form.binYield.value = formatYieldIntoDoubleDecimalMode ( openerform.binYield[0].value );
                        //form.binYield.value = openerform.binYield[0].value;
                    }
				}
                for( binCount = 0; binCount <= goodBinsCount; binCount++ )
                {
                    if( eval( 'openerform.bin_'+binCount) != null && eval('form.bin_'+binCount) != null )
                    {
                        if( eval('openerform.bin_'+binCount).length == null )
                        {
                            eval('form.bin_'+binCount).value = eval('openerform.bin_'+binCount).value;
                        }
                        else
                        {
                            eval('form.bin_'+binCount).value = eval('openerform.bin_'+binCount)[0].value;
                        }
                    }
                }
			}
		}
	}

	function CaculateDieOutAndShipYield()
	{
        form		= document.binDataEntryform;
		openerform	= window.opener.document.sortDataEntry;
        noOfWafers	= openerform.waferQuantity.value;
        goodBinsCount	= openerform.goodBinsCount.value;
        //grossDice		= openerform.grossDice.value;
        grossDice		= openerform.fabGrossDie.value;
        goodDieCount	= 0;

        if ( noOfWafers > 1 )
		{
			for( binRowCount = 0; binRowCount < noOfWafers;  binRowCount++ )
			{
				for( binCount = 0; binCount < goodBinsCount; binCount++ )
				{
					tempBinValue = eval('form.bin_'+binCount)[binRowCount];
					if ( tempBinValue != null && tempBinValue.value != "" &&  !isNaN( tempBinValue.value ) )
					{
						goodDieCount =  parseInt(goodDieCount) + parseInt(tempBinValue.value);
					}
				}
			}
        }
		else if( noOfWafers = 1)
		{
            for( binCount = 0; binCount < goodBinsCount; binCount++ )
			{
                tempBinValue = eval('form.bin_'+binCount);
                if ( tempBinValue != null && tempBinValue.value != "" &&  !isNaN( tempBinValue.value ) )
				{
                    goodDieCount =  parseInt( goodDieCount ) + parseInt( tempBinValue.value );
                }
            }
        }
        openerform.quantityOut.value	= parseInt( goodDieCount );
		openerform.avgGoodDie.value		= formatYieldIntoDoubleDecimalMode ( parseInt ( goodDieCount ) / parseInt( noOfWafers ) );
		/*
		The two params are added by Bhaskar on 14 Mar 2004 inorder to place a counter check on the
		changing of Wafer quantity and the Die Size fields before commiting in the sort data entry page.
		*/
		openerform.hiddenWaferQuantity.value = noOfWafers;
		//Bhaskar added this code block and commented the third on 2 Feb 2004
		var yieldValue = ( parseInt( goodDieCount ) * 100 )  /  ( grossDice * noOfWafers );
		yieldValue = ''+( Math.round( yieldValue * 100 ) )/100;
		var p1 = yieldValue.indexOf('.');
		if ( parseInt ( p1 ) == -1 )
		{
			yieldValue += '.00'
		}
		else if ( parseInt ( p1 ) != -1 )
		{
			var p2 = yieldValue.substring ( p1, yieldValue.length );
			if ( parseInt( p2.length ) < 3 )
			{
				yieldValue += '0';
			}
		}
        //openerform.yield.value = ( parseInt(goodDieCount)*100)  /  (grossDice * noOfWafers);
		openerform.yield.value = yieldValue;
    }

    function calculateYiled( binRowCount )
	{
        var pa	= document.getElementById( "message" );
        errors	= "";
		form		= document.binDataEntryform;
        openerform	= window.opener.document.sortDataEntry;

		noOfWafers		= openerform.waferQuantity.value;
        goodBinsCount	= openerform.goodBinsCount.value;
        //grossDice		= openerform.grossDice.value;
        grossDice		= openerform.fabGrossDie.value;
        goodDieCount	= 0;

		// Added by ravikanth to prevent JS error when the Die Size is not entered properly
		if ( noOfWafers > 1 )
		{
            for( var binCount = 0; binCount < goodBinsCount; binCount++ )
			{
                tempBinValue = eval('form.bin_'+binCount)[binRowCount];
                if ( tempBinValue != null && tempBinValue.value != "" &&  !isNaN( tempBinValue.value ) )
				{
                    goodDieCount =  parseInt( goodDieCount ) + parseInt( tempBinValue.value );
                }
				else if ( tempBinValue != null && tempBinValue.value != "" &&  isNaN( tempBinValue.value ) )
				{
                    errors = "Please Enter Bin_" + ( binCount + 1 ) + " Value in Correctly at Location for Sl.No " + ( binRowCount + 1 );
					//tempBinValue.focus();
					//return;
                }
			}
        }
		else
		{
            for( var binCount1 = 0; binCount1 < goodBinsCount; binCount1++ )
			{
                tempBinValue = eval('form.bin_'+binCount1);
                if ( tempBinValue != null && tempBinValue.value.trim() != "" &&  !isNaN( tempBinValue.value ) )
				{
                    goodDieCount =  parseInt( goodDieCount ) + parseInt( tempBinValue.value );
                }
				else if ( tempBinValue != null &&  tempBinValue.value.trim() != "" &&  isNaN(tempBinValue.value) )
				{
                    errors = "Please Enter Bin_" + (binCount1+1) + " Value in Correctly at Location for Sl.No"+(binRowCount+1);
                }
            }
        }

        if ( parseInt(goodDieCount,10) > parseFloat(grossDice) )
        {
            errors = "Bin total value can not be greater than Gross Die/Wafer(" + grossDice  + ") for Sl.No " + ( binRowCount + 1 );
        }

		if ( errors == '')
		{
			yield = parseInt(goodDieCount) / parseFloat(grossDice);
			if ( noOfWafers > 1)
			{
				var yieldValue = formatYieldIntoDoubleDecimalMode ( yield );
				form.binYield[binRowCount].value =yieldValue;
				var bin01Val = form.bin_0[binRowCount].value.trim();
                form.binRejected[binRowCount].value = eval(grossDice-goodDieCount);
			}
			else
			{
				form.binYield.value = formatYieldIntoDoubleDecimalMode ( yield );
				var bin11Val = form.bin_0.value.trim();
			}
		}

		if( errors != null )
		{
            pa.innerHTML = errors;
        }
    }

	function validateBinQuantities()
	{
		bool = true;
        var pa=document.getElementById("message");
		pa.innerHTML = "";
		openerform = window.opener.document.sortDataEntry;
		form = document.binDataEntryform;
		//grossDice = openerform.grossDice.value;
        grossDice		= openerform.fabGrossDie.value;
		noOfWafers = openerform.waferQuantity.value;
		if ( parseInt ( noOfWafers ) > 1 )
		{
			for( var i = 0; i < noOfWafers;  i++ )
			{
				bin1Val = form.bin_0[i].value.trim();
                if( bin1Val != "" )
                {
				    if ( checkInteger( bin1Val , ' Bin1 ' , 'Y'  ) == false)
				    {
					    pa.innerHTML  = errMsg+" for SlNo"+(i+1);
                        return false;
				    }
                }
			}
		}
		else if( noOfWafers = 1 )
		{
			bin1Val = form.bin_0.value.trim();
            if( bin1Val != "" )
            {
			    if ( checkInteger( bin1Val , ' Bin1 ' , 'Y'  ) == false )
			    {
				    pa.innerHTML  = errMsg;
				    return false;
			    }
            }
		}
		return true;
	}


 	/*
	Bhaskar added this function on 05 Feb 2004
	*/
	function formatYieldIntoDoubleDecimalMode ( yieldValu )
	{
		var yldValue	= yieldValu;
		yldValue		= '' + ( Math.round( yieldValu * 100 ) )/ 100;
		var p1			= yldValue.indexOf('.');

		if ( parseInt ( p1 ) == -1 )
		{
			yldValue += '.00'
		}
		else if ( parseInt ( p1 ) != -1 )
		{
			var p2 = yldValue.substring ( p1, yldValue.length );
			if ( parseInt( p2.length ) < 3 )
			{
				yldValue += '0';
			}
		}
		return yldValue;
	}
    </script>
</HEAD>
<%
    User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
    if ( user != null )
    {
%>
<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onLoad='fetchBinData();' >

<table align='center' border='0' width='100%' >
    <tr>
    <td align='center' class="NArialL">Sort Bin Data Entry</td>
    </tr>
    <tr>
    <td align='center'>&nbsp;</td>
    </tr>
    <form name="binDataEntryform" action="">
    <%
        int waferQty = 0;
        try
		{
            waferQty = Integer.parseInt( request.getParameter( "waferQty" ) );
        }
		catch( Exception ex )
		{
            waferQty = 0;
        }
        int goodBinsCount = 0;
        try
		{
            goodBinsCount = Integer.parseInt( request.getParameter( "goodBinsCount" ) );
        }
		catch( Exception ex )
		{
            goodBinsCount = 0;
        }

        if( waferQty == 0 )
		{
        %>
        <tr>
            <td align='center'><font class="NArialL" color="red">Wafer Qty Should be Greater zero / Not Entry </font></td>
        </tr>
        <tr>
            <td align='center'><font class="NArialL">&nsbp;</font></td>
        </tr>
        <tr>
            <td align='center'><a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></td>
        </tr>
        <%
        }
		else
		{
        %>
        <tr><td align='center'>
			<table align="center" border="0" width="80%">
				<tr>
				<td colspan="6" width="100%" class="NArialL">&nbsp;<FONT id="message" size=1 color='FF0000'></FONT></td>
				</tr>
                <tr> <td align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF"> SL.No </font></font></td>
                <td align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF"> Wafer Id *</font></font></td>
                <%
                    for( int binCount = 0; binCount < goodBinsCount; binCount++ )
				{
                %>
					<td align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Bin<%=binCount+1%>
					<%--<% if ( ( binCount + 1 ) == 1 ) { out.println ("*"); } %>--%>
					</font></font></td>
                <%
                }
                %>
                <td align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Bin Rejected</font></font></td>
                <td align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Yield</font></font></td>
                </tr>
                <%
                for( int count = 0; count < waferQty ; count++ )
                {
                %>
                  <tr>
                  <td align='center'><font class="NArialL"><%= count+1 %> </font></td>
                  <td align='center'><font class="NArialL"><input type="text" name="waferId" value="" size='10' maxlength='50' class="valuetxfield"  onkeypress="javascript:restrictCharacters(this)"></font></td>
				<%
					for( int binCount = 0; binCount < goodBinsCount; binCount++ )
					{
				%>
                      <td align='center'><font class="NArialL"> <input type="text" name='bin_<%=binCount%>' size='4' maxlength='4' class='valuetxfield' onkeypress="javascript:restrictCharacters(this)" <% if ( binCount != goodBinsCount){  %>  onChange="calculateYiled(<%=count%>)" <% } %> ></font></td>
                <%
					}
				%>
                      <td align='center'><font class="NArialL"> <input type="text" name='binRejected' size='8'  class='valuetxfield' onkeypress="javascript:restrictCharacters(this)" disabled></font></td>
                      <td align='center'><font class="NArialL"> <input type="text" name='binYield' size='8'  class='valuetxfield'  disabled='disabled'></font></td>
                </tr>
                <%
                }
                %>
                </table>
                </td></tr>
                <tr><td align='center'>&nbsp;</td></tr>
                <tr>
                <td align='center'>
                    <a href="javascript:save()" title='Click here to Save Sort Bin Data Entry'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:resetValues()" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a>
                </td>
                </tr>
                </form>
         <%
          }
        %>
</table>
</body>
<%
    }
    else{
    %>
    <BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' >
    <table cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <tr>
            <td>
                <table align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><font class="NArialL" ><font color="red"><b>Session has been time out , Please close this window and login again.<b></font></font></td></tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></td></tr>
                </table>
            </td>
       </tr>
       <tr><td>&nbsp;</td></tr>
    </table>
    </body>
<% } %>
</html>
